package com.lw.yygh.hosp.mq;

import com.alibaba.fastjson.JSONObject;
import com.lw.yygh.hosp.service.ScheduleService;
import com.lw.yygh.model.order.OrderMqVo;
import org.junit.jupiter.api.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class Client {
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private RedisTemplate redisTemplate;
    @JmsListener( destination = "${mytopic}")
    public void receive(TextMessage textMessage) throws JMSException {
        //判断消息是否重复消费
        Object obj = redisTemplate.opsForValue().get( textMessage.getJMSMessageID() );
        if (obj!=null){
            return;
        }
        String text = textMessage.getText();
        OrderMqVo orderMqVo = JSONObject.parseObject( text, OrderMqVo.class );
        //更新号源
        scheduleService.update(orderMqVo);
        //将消息id存入redis
        redisTemplate.opsForValue().set( textMessage.getJMSMessageID(),text,5, TimeUnit.MINUTES );
    }
}
